package com.ndood.spider.hqgz.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

import com.ndood.core.render.SeleniumDriverAction;
import com.ndood.core.util.WindowUtil;

@Component
public class HqgzAction implements SeleniumDriverAction {
	
	public static String tempSkuStr = "";
	// [{"attr_list":[{"attr_id":144,"attr_name":"原味包装（12片独立）"}],"num":20,"price":26,"no":"","pic":""},{"attr_list":[{"attr_id":145,"attr_name":"咸味蛋黄（双十二上新）"}],"num":9,"price":28,"no":"","pic":""}]
	public static List<Map<String,String>> list = new ArrayList<Map<String,String>>();

    @Override
    public void execute(WebDriver driver) {
    	try {
    		// 触发页面延时加载内容
    		WindowUtil.loadAll(driver);
			Thread.sleep(3000);
			
			// 点击播放视频
			WebElement button = getElementByCssSelector(driver, "#detail > div.tb-detail-bd.tb-clear > div.tb-summary.tb-clear > div > div.tb-item-info-l > div.tb-gallery > div > div.tb-video > div > div.vjs-center-container > button.vjs-center-start.vjs-button");
			// #detail > div.tb-detail-bd.tb-clear > div.tb-summary.tb-clear > div > div.tb-item-info-l > div.tb-gallery > div > div.tb-video > div > div.vjs-center-container > button.vjs-center-start.vjs-button
			// #detail > div.tb-detail-bd.tb-clear > div.tb-summary.tb-clear > div > div.tb-item-info-l > div.tb-gallery > div > div.tb-video > div > div.vjs-center-container > button.vjs-center-start.vjs-button
			if(button!=null){
				button.click();
				Thread.sleep(1000);
			}
			
			// 处理规格点击事件 
			List<WebElement> skus = getElementsByCssSelector(driver,"#J_isku > div > dl.J_Prop.tb-prop.tb-clear > dd > ul > li");
			list = new ArrayList<Map<String,String>>();
			for (int i=0;i<skus.size();i++) {
				WebElement sku = skus.get(i);
				String skuName = getElementTextByCssSelector(sku,"a > span");
				sku.click();
				Thread.sleep(300);
				String price = getElementTextByCssSelector(driver, "#J_StrPrice > em.tb-rmb-num");
				String nowPrice = getElementTextByCssSelector(driver, "#J_PromoPriceNum");
				if(nowPrice==null){
					nowPrice=price;
				}
				String stock = getElementTextByCssSelector(driver,"#J_SpanStock");
				System.out.println("规格"+i+"："+skuName+"-原价："+price+"-现价："+nowPrice+"-库存："+stock);
				tempSkuStr +=  "规格"+i+"："+skuName+"-原价："+price+"-现价："+nowPrice+"-库存："+stock;
				
				Map<String,String> map = new HashMap<String,String>();
				map.put("attrName", skuName);
				map.put("price", nowPrice);
				map.put("num", stock);
				list.add(map);
			}
			
    	} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public WebElement getElementByCssSelector(SearchContext e, String selector){
    	try {
    		WebElement element = e.findElement(By.cssSelector(selector));
    		return element;
		} catch (Exception e2) {
			return null;
		}
    }
    
    public String getElementTextByCssSelector(SearchContext e, String selector){
    	try {
    		String text = e.findElement(By.cssSelector(selector)).getText();
    		return text;
		} catch (Exception e2) {
			return null;
		}
    }
    
    public List<WebElement> getElementsByCssSelector(SearchContext e, String selector){
    	try {
    		List<WebElement> list = e.findElements(By.cssSelector(selector));
    		return list;
		} catch (Exception e2) {
			return new ArrayList<WebElement>();
		}
    }
}
